package cn.xscrum.vtsp.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.xscrum.common.core.text.Convert;
import cn.xscrum.common.utils.StringUtils;
import cn.xscrum.vtsp.domain.*;
import cn.xscrum.vtsp.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.utils.poi.ExcelUtil;
import cn.xscrum.common.core.page.TableDataInfo;

/**
 * 入队申请Controller
 *

 * 志愿者信息Controller
 *
 * @author xscrum
 * @date 2021-10-01
 */
@Controller
@RequestMapping("/vtsp/volunteer")
public class VtspVolunteerController extends BaseController
{
    private String prefix = "vtsp/volunteer";

    @Autowired
    private IVtspVolunteerService vtspVolunteerService;

    @Autowired
    private IVtspSectorService vtspSectorService;
    @Autowired
    private IVtspEnrollService enrollService;
    @Autowired
    private IVtspAppraiseService appraiseService;

    @Autowired
    private IVtspAcademyService vtspAcademyService;
    @Autowired
    private IVtspPostService postService;

    @RequiresPermissions("vtsp:volunteer:view")
    @GetMapping()
    public String volunteer(Model model) {
        List<VtspSector> sectorList = vtspSectorService.selectVtspSectorList(new VtspSector());
        List<VtspAcademy> academyList = vtspAcademyService.selectVtspAcademyList(new VtspAcademy());
        model.addAttribute("sectorList", sectorList);
        model.addAttribute("academyList", academyList);
        return prefix + "/volunteer";
    }

    /**
     * 查询志愿者信息列表
     */
    @RequiresPermissions("vtsp:volunteer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VtspVolunteer vtspVolunteer)
    {
        startPage();
        if(vtspVolunteer.getEndDate() != null) {
            //  Date -> 时间戳
            long timeStamp = vtspVolunteer.getEndDate().getTime();
            //  时间戳 + (一天的秒数 - 1) * 1000
            timeStamp += (24 * 60 * 60 - 1) * 1000;
            //  时间戳 -> Date
            vtspVolunteer.setEndDate(new Date(timeStamp));
        }
        //  根据vtspAdvice对象的属性查询
        List<VtspVolunteer> list = vtspVolunteerService.selectVtspVolunteerList(vtspVolunteer);
        return getDataTable(list);
    }
    /**
     * 查询志愿者信息列表
     */
    @RequiresPermissions("vtsp:volunteer:list")
    @PostMapping("/list/{activityId}")
    @ResponseBody
    public TableDataInfo listA(@PathVariable Long activityId, VtspVolunteer vtspVolunteer)
    {
        VtspEnroll enroll = new VtspEnroll();
        enroll.setActivityId(activityId);
        enroll.setEnrollState(6);
        List<VtspEnroll> enrolls = enrollService.selectVtspEnrollList(enroll);

        List<VtspVolunteer> list = new ArrayList<>();
        VtspAppraise appraise = new VtspAppraise();
        for (VtspEnroll vtspEnroll : enrolls) {
            appraise.setEnrollId(vtspEnroll.getEnrollId());
            appraise.setAppraiseState(vtspVolunteer.getAppraiseState());
            List<VtspAppraise> appraiseList = appraiseService.list(appraise);
            for (VtspAppraise vtspAppraise : appraiseList) {
                VtspEnroll enroll1 = enrollService.selectVtspEnrollById(vtspAppraise.getEnrollId());
                VtspVolunteer volunteer = vtspVolunteerService.selectVtspVolunteerById(enroll1.getVolunteerId());
                volunteer.setAppraiseState(vtspVolunteer.getAppraiseState());
                Long postId = vtspEnroll.getPostId();
                String postName = postService.selectVtspPostById(postId).getPostName();
                volunteer.setPostName(postName);
                list.add(volunteer);
            }
        }

        return getDataTable(list);
    }

    /**
     * 导出志愿者信息列表
     */
    @RequiresPermissions("vtsp:volunteer:export")
    @Log(title = "志愿者信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(VtspVolunteer vtspVolunteer)
    {
        List<VtspVolunteer> list = vtspVolunteerService.selectVtspVolunteerList(vtspVolunteer);
        ExcelUtil<VtspVolunteer> util = new ExcelUtil<VtspVolunteer>(VtspVolunteer.class);
        return util.exportExcel(list, "volunteer");
    }

    /**
     * 新增志愿者信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    @GetMapping("/detail/{volunteerId}")
    public String detail(@PathVariable("volunteerId") Long volunteerId, ModelMap mmap) {
        VtspVolunteer vtspVolunteer = vtspVolunteerService.selectVtspVolunteerById(volunteerId);
        mmap.put("vtspVolunteer", vtspVolunteer);
        return prefix + "/detail";
    }
    /**
     * 已评价
     */
    @GetMapping("/appraiseModel/{activityId}/{id}")
    public String appraiseModel(@PathVariable Long activityId, @PathVariable Long id, ModelMap mmap) {
        VtspEnroll enroll = new VtspEnroll();
        enroll.setActivityId(activityId);
        enroll.setVolunteerId(id);
        List<VtspEnroll> enrolls = enrollService.selectVtspEnrollList(enroll);
        VtspAppraise appraise = new VtspAppraise();
        Long enrollId = enrolls.get(0).getEnrollId();
        appraise.setEnrollId(enrollId);
        List<VtspAppraise> appraiseList = appraiseService.selectVtspAppraiseList(appraise);
        mmap.addAttribute("enrollId", enrollId);
        mmap.addAttribute("appraiseId", appraiseList.get(0).getAppraiseId());
        return prefix + "/appraiseModel";
    }
    /**
     * 未评价
     */
    @GetMapping("/appraiseModel2/{activityId}/{id}")
    public String appraiseModel2(@PathVariable Long activityId, @PathVariable Long id, ModelMap mmap) {
        VtspEnroll enroll = new VtspEnroll();
        enroll.setActivityId(activityId);
        enroll.setVolunteerId(id);
        List<VtspEnroll> enrolls = enrollService.selectVtspEnrollList(enroll);
        Long enrollId = enrolls.get(0).getEnrollId();

        VtspAppraise appraise = new VtspAppraise();
        appraise.setEnrollId(enrollId);
        appraise.setVolunteerId(id.toString());
        List<VtspAppraise> appraiseList = appraiseService.selectVtspAppraiseList(appraise);
        mmap.addAttribute("appraise", appraiseList.get(0));
        return prefix + "/appraiseModel2";
    }
    /**
     * 新增保存志愿者信息
     */
    @RequiresPermissions("vtsp:volunteer:add")
    @Log(title = "志愿者信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(VtspVolunteer vtspVolunteer)
    {
        return toAjax(vtspVolunteerService.insertVtspVolunteer(vtspVolunteer));
    }

    /**
     * 修改志愿者信息
     */
    @GetMapping("/edit/{volunteerId}")
    public String edit(@PathVariable("volunteerId") Long volunteerId, Model model, ModelMap mmap)
    {
        VtspVolunteer vtspVolunteer = vtspVolunteerService.selectVtspVolunteerById(volunteerId);
        List<VtspSector> sectorList = vtspSectorService.selectVtspSectorList(new VtspSector());
        mmap.put("vtspVolunteer", vtspVolunteer);
        model.addAttribute("sectorList", sectorList);
        return prefix + "/edit";
    }

    /**
     * 修改保存志愿者信息
     */
    @RequiresPermissions("vtsp:volunteer:edit")
    @Log(title = "志愿者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VtspVolunteer vtspVolunteer)
    {
        return toAjax(vtspVolunteerService.updateVtspVolunteer(vtspVolunteer));
    }

    /**
     * 删除志愿者信息
     */
    @RequiresPermissions("vtsp:volunteer:remove")
    @Log(title = "志愿者信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(vtspVolunteerService.deleteVtspVolunteerByIds(ids));
    }


    /* 框架自动生成以外的部分 */

    /**
     * 批量拒绝入队申请
     * memberState ? -> 3, 并且附带拒绝信息
     */
    @RequiresPermissions("vtsp:volunteer:refuseAll")
    @Log(title = "志愿者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/refuseAll")
    @ResponseBody
    public AjaxResult refuseAll(String ids, String memo) {
        return toAjax(vtspVolunteerService.refuseVtspVolunteerByIds(ids, memo));
    }

    /**
     * 批量同意入队申请
     * memberState ? -> 6
     */
    @RequiresPermissions("vtsp:volunteer:acceptAll")
    @Log(title = "志愿者信息", businessType = BusinessType.UPDATE)
    @PostMapping("/acceptAll")
    @ResponseBody
    public AjaxResult acceptAll(String ids) {
        return toAjax(vtspVolunteerService.acceptVtspVolunteerByIds(ids));
    }
}